<?php


namespace EchoPHP\Database\Connectors;


use PDO;

class OracleConnector extends Connector implements ConnectorInterface {

    /**
     * 创建ORACLE连接
     * @param array $config
     * @return PDO
     */
    public function connect(array $config) {
        $dsn = $this->getDsn($config);
        $userName = $config['username'] ?? '';
        $password = $config['password'] ?? '';
        $options = $config['options'] ?? [];

        $this->addDefaultOptions([
            PDO::ATTR_EMULATE_PREPARES => false,            //禁用预处理模拟
        ]);
        $options = $this->getOptions($options);
        return $this->createConnection($dsn, $userName, $password, $options);
    }

    /**
     * 获取dsn
     * @param array $config
     * @return string
     */
    protected function getDsn(array $config) {
        $dsn = 'oci:dbname=';
        if (isset($config['host']) && !empty($config['host'])) {
            //  Oracle Instant Client
            $dsn .= '//' . $config['host'] . ':' . ($config['port'] ?? 1521) . '/';
        }
        $dsn .= $config['database'];
        if (!empty($config['charset'])) {
            $dsn .= ';charset=' . $config['charset'];
        }
        return $dsn;
    }

}